$(function () {
    getWebSite();
    setHeader();
    const dropdown = layui.dropdown;
    dropdown.render({
        elem: "#dropMenu",
        data: [{
            title: "我的主页",
            id: "user",
        }, {
            title: "我的关注",
            id: "follow"
        }, {
            title: "我的点赞",
            id: "like"
        }, {
            title: "我的收藏",
            id: "collection"
        }, {
            type: '-' // 分割线
        }, {
            title: "修改密码",
            id: "password"
        }, {
            title: "退出登录",
            id: "logout"
        }],
        click(data) {
            const loginInfo = getLoginInfo();
            switch (data.id) {

                case "user":
                    if (loginInfo.userinfoId) {
                        location.href = "page/user/info/" + loginInfo.userinfoId + "#tabId=info"
                    } else {
                        layer.msg('请先登录！', {icon: 6})
                    }
                    break;
                case "follow":
                    if (loginInfo.userinfoId) {
                        location.href = "page/user/info/" + loginInfo.userinfoId + "#tabId=followed"
                    } else {
                        layer.msg('请先登录！', {icon: 6})
                    }
                    break;
                case "like":
                    if (loginInfo.userinfoId) {
                        location.href = "page/user/info/" + loginInfo.userinfoId + "#tabId=like"
                    } else {
                        layer.msg('请先登录！', {icon: 6})
                    }
                    break;
                case "collection":
                    if (loginInfo.userinfoId) {
                        location.href = "page/user/info/" + loginInfo.userinfoId + "#tabId=collection"
                    } else {
                        layer.msg('请先登录！', {icon: 6})
                    }
                    break;
                case "logout":
                    layer.confirm('确定退出登录吗?', {icon: 3, title: "询问"}, function (index) {
                        http.get("logout", null, function () {
                            layer.msg('注销成功！', {icon: 6})
                            Cookies.remove("token");
                            location.reload();
                        })
                        layer.close(index);
                    })
                    break;
                case "password":
                    layer.open({
                        type: 2,
                        content: "page/password",
                        area: ["400px", "500px"],
                        title: "修改密码"
                    })
                    break;
            }
        }
    });
    /*发布文章按钮*/
    $(".apply-btn").click(function () {
        if (getLoginInfo()) {
            window.open('apply', '_blank')
        } else {
            layer.alert('请先登录！', {icon: 5});
        }
    });
    /*搜索按钮单击*/
    $("#searchBtn").click(function () {
        searchBlog();
    });
    /*搜索框回车*/
    $("#search").keydown(function (ev) {
        if (ev.keyCode === 13) {
            searchBlog();
        }
    })

})

function searchBlog() {
    const search = $("#search").val();
    if (search) {
        location.href = "page/search?search=" + search;
    }
}

function showLoginWin(index) {
    layer.open({
        type: 2,
        content: "page/vip?type=" + index,
        area: ["400px", "500px"],
        title: "登录选项"
    })
}

function getWebSite() {
    http.get("blog/website", null, function (res) {
        document.title = res.data.name
        $("#logo").prop("src", "static/upload/" + res.data.logo).prop("title", res.data.name)
        $("#copy").text(res.data.copy)
        $("#address").text(res.data.address)
        $("#phone").text(res.data.phone)
    })
}

function getLoginInfo() {
    let loginInfo;
    $.ajax({
        url: "login/info",
        method: "get",
        headers,
        async: false,
        dataType: "json",
        success(res) {
            if (res.code === 200) {
                loginInfo = {...res.data}
            }
        },
        error(e) {
            layer.alert('与服务器断开连接...');
            console.log(e)
        }
    })
    return loginInfo;
}


function setHeader() {
    const vipInfo = getLoginInfo();
    if (vipInfo) {
        $("#vipName").text(vipInfo.nickname)
        $("#vipAvatar").prop('src', 'static/upload/' + vipInfo.avatar)
        $(".user .avatar").css('display', 'flex');
        $(".user .login").css('display', 'none');
    } else {
        $(".user .avatar").css('display', 'none');
        $(".user .login").css('display', 'flex');
    }
}
